package jinyilw.swing.rlaf.theme;

import jinyilw.swing.ColorTools;

import javax.swing.plaf.UIResource;
import javax.swing.text.JTextComponent;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Paint;

public class RThemeColor
{
	public static final Color hiBackColor = new Color(240, 240, 245);
	public static final Color loBackColor = new Color(182, 185, 190);

	public static final Color darkBorderColor = new Color(0, 0, 0, 130);
	public static final Color lightBorderColor = new Color(150, 150, 150, 100);

	public static final RColorSet colorSet = RColorSet.getColorSet();
	private static Color[] defaultColors, rolloverColors;
	private static Color[] gradientColors;

	public static Color themeColor = Color.cyan;
	public static Color windowBorderColor;
	public static Color darkThemeColor;
	public static Color transThemeColor, transThemeColor2;
	public static Color selectedTextColor = Color.white;
	private static Color[] gradualTransColors, circleTransColors;

	static
	{
		themeColor = ColorTools.getColorFromDefaultSettings("themeColor", themeColor);
		transThemeColor2 = windowBorderColor = ColorTools
				.getAlphaColor(themeColor, 180);
		darkThemeColor = themeColor.darker();
		transThemeColor = ColorTools.getAlphaColor(themeColor, 120);
	}

	private RThemeColor()
	{
	}

	public static void customForegroundSetting(Component c)
	{
		Color fColor = c.getForeground();
		if (fColor instanceof UIResource && fColor != colorSet.textColor)
			c.setForeground(colorSet.textColor);
	}

	public static void customTextColorSetting(JTextComponent t)
	{
		if (t.getSelectionColor() != darkThemeColor)
			t.setSelectionColor(darkThemeColor);
		if (t.getCaretColor() != colorSet.textColor)
			t.setCaretColor(colorSet.textColor);
		Color fColor = t.getForeground();
		if (fColor instanceof UIResource && fColor != colorSet.textColor)
			t.setForeground(colorSet.textColor);
	}

	public static void setThemeColor(Color color)
	{
		if (color == null || color.equals(themeColor))
			return;
		themeColor = color;
		transThemeColor2 = windowBorderColor = ColorTools
				.getAlphaColor(themeColor, 180);
		darkThemeColor = themeColor.darker();
		transThemeColor = ColorTools.getAlphaColor(themeColor, 120);
		if (gradualTransColors != null)
		{
			gradualTransColors = null;
			getGradualTransColors();
		}
		if (circleTransColors != null)
		{
			circleTransColors = null;
			getCircleTransColors();
		}
	}

	public static Color[] getGradualTransColors()
	{
		if (gradualTransColors == null)
		{
			gradualTransColors = ColorTools.getGradualColors(
					transThemeColor.darker(), transThemeColor,
					transThemeColor.brighter(), 5, 5);
		}
		return gradualTransColors;
	}

	public static Color[] getCircleTransColors()
	{
		if (circleTransColors == null)
		{
			circleTransColors = ColorTools.getGradualColors(transThemeColor,
					themeColor, transThemeColor, 8, 8);
		}
		return circleTransColors;
	}

	public static Color[] getDefaultColors()
	{
		if (defaultColors == null)
			defaultColors = new Color[]{new Color(106, 150, 192),
					new Color(154, 190, 209), new Color(182, 208, 231),
					new Color(200, 223, 255), new Color(189, 218, 246),
					new Color(167, 204, 231), new Color(148, 191, 226),
					new Color(144, 181, 225), new Color(145, 182, 226),
					new Color(151, 188, 230), new Color(160, 198, 235),
					new Color(168, 206, 242), new Color(174, 213, 244),
					new Color(183, 222, 251), new Color(191, 230, 255),
					new Color(202, 237, 255), new Color(206, 247, 253),
					new Color(211, 255, 254), new Color(208, 255, 254),
					new Color(206, 249, 255), new Color(202, 237, 255)};
		return defaultColors;
	}

	public static Color[] getRolloverColors()
	{
		if (rolloverColors == null)
			rolloverColors = new Color[]{new Color(106, 192, 150),
					new Color(154, 209, 190), new Color(173, 220, 198),
					new Color(182, 232, 203), new Color(180, 234, 207),
					new Color(167, 231, 204), new Color(148, 226, 191),
					new Color(144, 225, 181), new Color(145, 226, 182),
					new Color(151, 230, 188), new Color(160, 235, 198),
					new Color(168, 242, 206), new Color(174, 244, 213),
					new Color(183, 251, 222), new Color(191, 255, 230),
					new Color(202, 255, 237), new Color(206, 253, 247),
					new Color(211, 254, 255), new Color(208, 254, 255),
					new Color(206, 249, 255), new Color(196, 247, 227)};
		return rolloverColors;
	}

	public static Color[] getGradientColors()
	{
		if (gradientColors == null)
			gradientColors = ColorTools.createGradientColors(hiBackColor,
					loBackColor, 48);
		return gradientColors;
	}

	public static Paint createVerGradientPaint(int x, int y, int w, int h)
	{
		return new GradientPaint(x, y, hiBackColor, x + w, y, loBackColor);
	}

	public static Paint createHorBorderPaint(int x, int y, int w, int h)
	{
		return new GradientPaint(x, y, lightBorderColor, x, y + h,
				darkBorderColor);
	}
}
